#coding:utf-8

from elasticsearch import Elasticsearch
import datetime

DOCUMENT = {
    'NAME':{
        'type':'string',
        'index':'not_analyzed',
    },
    'URL':{
        'type':'string',
        'index':'not_analyzed',
    },
    'SOURCE':{
        'type':'string',
        'index':'not_analyzed',
    },
    'TITLE':{
        'type':'string',
        'index':'no'
    },
    'CONTENT':{
        'type':'string',
        'index':'no'
    },
    'CUT_TITLE':{
        'type':'string',
        'index':'analyzed',
        'analyzer':'whitespace'
    },
    'CUT_CONTENT':{
        'type':'string',
        'index':'analyzed',
        'analyzer':'whitespace'
    },
    'TIME':{
        'type':'date',
        'format':'YYYY-MM-dd HH:mm:ss',
    },
    'KEYWORD':{
        'type':'string',
        'index':'no'
    }
}

DOCUMENT_TYPE = {'properties':DOCUMENT}

class EsStore(object):

    def __init__(self,prop):
        self.es = Elasticsearch(hosts=[{'host':prop['HOST'],'port':prop['PORT']}])

    def init(self,index_name,type_name,type_schema,ns=1,nr=0):
        s = {
            'settings':{
                'index': {
                    'number_of_shards':ns,
                    'number_of_replicas':nr
                    }
                }
        }
        self.es.indices.create(index=index_name,body=s)
        self.es.indices.put_mapping(index=index_name,doc_type=type_name,body=type_schema)

    def clean(self,index_name, type_name):
        self.es.indices.delete_mapping(index=index_name,doc_type=type_name)
        self.es.indices.delete(index=index_name)


class EsTestStore(object):

    def __init__(self,prop):
        self.es = Elasticsearch(hosts=[{'host':prop['HOST'],'port':prop['PORT']}]) 
        self.index = prop['INDEX']
        self.type = prop['TYPE']

    def get(self,i,fields='*'):
        res = self.es.get(index=self.index,doc_type=self.type,id=i,_source_include=fields)
        return res['_source']

    def insert(self,i,m):
        self.es.index(index=self.index,doc_type=self.type,id=i,body=m)

    def getMaxTime(self):
        q = {
            "aggs": {
                "max_time": {"max": {"field": "TIME"} }
            }
        }
        res = self.es.search(index=self.index,body=q,_source_include='*')
        res = res['aggregations']
        res = res['max_time']
        res = res['value']
        res = float(res)/1000.0
        res = datetime.datetime.fromtimestamp(res)
        res = res-datetime.timedelta(hours=8)
        return res

    def getByTime(self, st, et, fields='*'):
        T1 = st.strftime('%Y-%m-%d %H:%M:%S')
        T2 = et.strftime('%Y-%m-%d %H:%M:%S')
        q = {'range':{
            'TIME':{
                "from": T1,
                "to": T2
            }
        }}

        q = {"query":{
            "bool": {
                "must":[q]
            }
        }}        

        res = self.es.search(index=self.index,doc_type=self.type,\
                                body=q,scroll='10m',search_type='scan',\
                                _source_include=fields,size=500)
        si = res['_scroll_id']
        ct = res['hits']['total']
        return {'si':si,'ct':ct}

    def getAll(self,fields=['ID','ENTITY','FEATURE']):
        q = {"query":{"match_all":{}}}

        res = self.es.search(index=self.index,doc_type=self.type,\
                                body=q,scroll='10m',search_type='scan',\
                                _source_include=fields,size=500)
        si = res['_scroll_id']
        ct = res['hits']['total']
        return {'si':si,'ct':ct}

    def next(self,si):
        res = self.es.scroll(scroll_id=si,scroll='10m')
        si = res['_scroll_id']
        res = res['hits']['hits']
        res = [r['_source'] for r in res]
        return {'si':si,'rs':res}  

def initEsTestStore():
    prop = {}
    prop['HOST'] = '10.4.21.63'
    prop['PORT'] = 9250
    prop['INDEX'] = 'test_data'
    prop['TYPE'] = 'test_data'

    es = EsTestStore(prop)
    return es

def initEsDataStore():
    prop = {}
    prop['HOST'] = '10.4.21.59'
    prop['PORT'] = 9250
    prop['INDEX'] = 'data_group'
    prop['TYPE'] = 'data_group'

    es = EsTestStore(prop)
    return es




if __name__ == '__main__':

    print 'Hi'

    prop = {} 
    prop['HOST'] = '10.4.21.63'
    prop['PORT'] = 9250

    es = EsStore(prop)

    import sys
    i = sys.argv[1]
    if i == 'init':
        es.init('test_data','test_data',DOCUMENT_TYPE,1,0)
    if i == 'remove':
        es.clean('test_data','test_data')

